package www.openteam.com.ui.business.other;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.cundong.recyclerview.EndlessRecyclerOnScrollListener;
import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.adapter.other.CustomPMCInfoAdapter;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.CustomType;
import www.openteam.com.contract.business.other.SearchCustomPMCContract;
import www.openteam.com.presenter.business.other.SearchPMCPresenter;
import www.openteam.com.tool.RecyclerViewStateUtils;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.widget.LoadingFooter;

/**
 * Created by kylin on 2016/11/30.
 */
@SuppressLint("ValidFragment")
public class SearchCustomPMCFragment extends mBaseFragment<SearchPMCPresenter>
        implements SearchCustomPMCContract.SearchPMCInfoView {


    private final String TAG = SearchCustomPMCFragment.class.getSimpleName();
    private RecyclerView mRecyclerView;
    private LinearLayoutManager linearLayoutManager;
    private CustomPMCInfoAdapter customPMCInfoAdapter;
    int pageSize = 0;
    int i = 0;
    private Throwable throwable;
    private ArrayList<CustomType.DataBean> CustomPMC;
    private ArrayList<CustomType.DataBean> handleCustomPMC;
    String name = "";
    @Bind(R.id.et_search)
    EditText et_search;

    @Bind(R.id.iv_search)
    ImageView iv_search;
    private HeaderAndFooterRecyclerViewAdapter mFooterRecyclerViewAapter;
    private ButtonSelect buttonSelect;
    int type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public SearchCustomPMCFragment(int type) {
        this.type = type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, view);
        mPresenter = loadPresenter();
        initCommonData();
        initView();
        initListener();
        initData();

        return view;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_search;
    }

    private void initCommonData() {
        if (mPresenter != null)
            mPresenter.attachView(this);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        switch (type) {
            case 0:
                iv_search.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createProgressDialog(getResources().getString(R.string.waiting));
                        if (customPMCInfoAdapter == null) {
                            CustomPMC = new ArrayList<CustomType.DataBean>();
                            handleCustomPMC = new ArrayList<CustomType.DataBean>();
                            mPresenter.queryMoneyTypeInfo(pageSize);
                        } else {
                            customPMCInfoAdapter.deleteItems(CustomPMC);
                            mRecyclerView.removeAllViews();
                            CustomPMC.clear();
                            throwable = null;
                            i = 0;
                            handleCustomPMC.clear();
                        }
                    }
                });
                initRView(view);
                mRecyclerView.addOnScrollListener(mOnScrollListener);
                break;
            case 1:
                iv_search.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createProgressDialog(getResources().getString(R.string.waiting));
                        name = et_search.getText().toString();
                        if (customPMCInfoAdapter == null) {
                            CustomPMC = new ArrayList<CustomType.DataBean>();
                            handleCustomPMC = new ArrayList<CustomType.DataBean>();
                            mPresenter.queryCopayTypeInfo(pageSize);
                        } else {
                            customPMCInfoAdapter.deleteItems(CustomPMC);
                            mRecyclerView.removeAllViews();
                            CustomPMC.clear();
                            throwable = null;
                            i = 0;
                            handleCustomPMC.clear();
                        }
                    }
                });
                initRView(view);
                mRecyclerView.addOnScrollListener(cOnScrollListener);
                break;
            case 2:

                iv_search.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createProgressDialog(getResources().getString(R.string.waiting));
                        name = et_search.getText().toString();
                        if (customPMCInfoAdapter == null) {
                            CustomPMC = new ArrayList<CustomType.DataBean>();
                            handleCustomPMC = new ArrayList<CustomType.DataBean>();
                            mPresenter.queryCocustomTypeInfo(pageSize);
                        } else {
                            customPMCInfoAdapter.deleteItems(CustomPMC);
                            mRecyclerView.removeAllViews();
                            CustomPMC.clear();
                            throwable = null;
                            i = 0;
                            handleCustomPMC.clear();
                        }
                    }
                });
                initRView(view);
                mRecyclerView.addOnScrollListener(sOnScrollListener);
                break;
            case 3:
                iv_search.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createProgressDialog(getResources().getString(R.string.waiting));
                        name = et_search.getText().toString();
                        if (customPMCInfoAdapter == null) {
                            CustomPMC = new ArrayList<CustomType.DataBean>();
                            handleCustomPMC = new ArrayList<CustomType.DataBean>();
                            mPresenter.querySendTypeInfo(pageSize);
                        } else {
                            customPMCInfoAdapter.deleteItems(CustomPMC);
                            mRecyclerView.removeAllViews();
                            CustomPMC.clear();
                            throwable = null;
                            i = 0;
                            handleCustomPMC.clear();
                        }
                    }
                });
                initRView(view);
                mRecyclerView.addOnScrollListener(seOnScrollListener);
                break;
        }

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected SearchPMCPresenter loadPresenter() {
        return new SearchPMCPresenter();
    }


    public EndlessRecyclerOnScrollListener cOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            if (state == LoadingFooter.State.Loading) {
                i++;
            }
            i++;
            if (throwable == null && (CustomPMC.size() % 10 == 0)) {

                CustomPMC = new ArrayList<CustomType.DataBean>();
                handleCustomPMC = new ArrayList<CustomType.DataBean>();
                mPresenter.queryCopayTypeInfo(i);

                RecyclerViewStateUtils.
                        setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            } else if (throwable == null && (CustomPMC.size() % 10 != 0)) {
                //the end
                mPresenter.queryCopayTypeInfo(i);
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            } else if (throwable != null) {
                //the end
                i = 0;
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            }

        }
    };


    public EndlessRecyclerOnScrollListener sOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            if (state == LoadingFooter.State.Loading) {
                i++;
            }
            i++;
            if (throwable == null && (CustomPMC.size() % 10 == 0)) {

                CustomPMC = new ArrayList<CustomType.DataBean>();
                handleCustomPMC = new ArrayList<CustomType.DataBean>();
                mPresenter.queryCocustomTypeInfo(i);

                RecyclerViewStateUtils.
                        setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            } else if (throwable == null && (CustomPMC.size() % 10 != 0)) {
                //the end
                Log.d(TAG, "onLoadNextPage:  !-9 ");
                mPresenter.queryCocustomTypeInfo(i);
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            } else if (throwable != null) {
                //the end
                i = 0;

                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            }

        }
    };

    public EndlessRecyclerOnScrollListener seOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            if (state == LoadingFooter.State.Loading) {
                i++;
            }
            i++;
            if (throwable == null && (CustomPMC.size() % 10 == 0)) {

                CustomPMC = new ArrayList<CustomType.DataBean>();
                handleCustomPMC = new ArrayList<CustomType.DataBean>();
                mPresenter.querySendTypeInfo(i);

                RecyclerViewStateUtils.
                        setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            } else if (throwable == null && (CustomPMC.size() % 10 != 0)) {
                //the end
                mPresenter.querySendTypeInfo(i);
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            } else if (throwable != null) {
                //the end
                i = 0;

                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            }

        }
    };

    public EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            if (state == LoadingFooter.State.Loading) {
                Log.d("@Cundong", "the state is Loading, just wait..");
                i++;
            }
            i++;
            if (throwable == null && (CustomPMC.size() % 10 == 0)) {

                Log.d(TAG, "onLoadNextPage: jx");
                CustomPMC = new ArrayList<CustomType.DataBean>();
                handleCustomPMC = new ArrayList<CustomType.DataBean>();
                mPresenter.queryMoneyTypeInfo(i);

                RecyclerViewStateUtils.
                        setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            } else if (throwable == null && (CustomPMC.size() % 10 != 0)) {
                //the end
                Log.d(TAG, "onLoadNextPage:  !-9 ");
                mPresenter.queryMoneyTypeInfo(i);
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            } else if (throwable != null) {
                //the end
                i = 0;
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            }

        }
    };

    private void setCustomPMCsAdapter() {
        if (customPMCInfoAdapter == null) {
            customPMCInfoAdapter = new CustomPMCInfoAdapter(getActivity());
            customPMCInfoAdapter.addItems(CustomPMC);
            customPMCInfoAdapter.addContext(getActivity());
            customPMCInfoAdapter.cButtonSetOnclick(new CustomPMCInfoAdapter.CButtonTouchEvent() {
                @Override
                public void doubleClickSelect(CustomType.DataBean CustomPMC) {
                    buttonSelect.buttonOnclickSelect(CustomPMC, type);
                }
            });
            mFooterRecyclerViewAapter = new HeaderAndFooterRecyclerViewAdapter(customPMCInfoAdapter);
            mRecyclerView.setAdapter(mFooterRecyclerViewAapter);
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        } else {
            mFooterRecyclerViewAapter.notifyDataSetChanged();
            customPMCInfoAdapter.addItems(CustomPMC);
            customPMCInfoAdapter.addContext(getActivity());
            customPMCInfoAdapter.cButtonSetOnclick(new CustomPMCInfoAdapter.CButtonTouchEvent() {
                @Override
                public void doubleClickSelect(CustomType.DataBean CustomPMC) {
                    buttonSelect.buttonOnclickSelect(CustomPMC, type);
                }
            });
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        }
    }


    private View.OnClickListener mFooterClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            mPresenter.queryMoneyTypeInfo(i);
        }
    };

    @Override
    public void loadCTIData(CustomType customType) {
        mProgressDialog.dismiss();
        for (CustomType.DataBean dev : customType.getData()) {
            this.CustomPMC.add(dev);
            handleCustomPMC.add(dev);
        }
        setCustomPMCsAdapter();
    }

    @Override
    public void hideCTIData(Throwable throwable) {
        mProgressDialog.dismiss();
        this.throwable = throwable;
        RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
        ToastUtil.showToast(getActivity(), throwable.getMessage());
//            RecyclerViewStateUtils.setFooterViewState(getActivity(), this.mRecyclerView, REQUEST_COUNT, LoadingFooter.State.NetWorkError, this.mFooterClick);
//            ToastUtil.showToast(getActivity(), "网络连接有误");
    }

    @Override
    public void loadCpTIData(CustomType customType) {
        mProgressDialog.dismiss();
        for (CustomType.DataBean dev : customType.getData()) {
            this.CustomPMC.add(dev);
            handleCustomPMC.add(dev);
        }
        setCustomPMCsAdapter();
    }


    @Override
    public void loadMTIData(CustomType customType) {
        mProgressDialog.dismiss();
        for (CustomType.DataBean dev : customType.getData()) {
            this.CustomPMC.add(dev);
            handleCustomPMC.add(dev);
        }
        setCustomPMCsAdapter();
    }

    @Override
    public void loadSTIData(CustomType customType) {
        mProgressDialog.dismiss();
        for (CustomType.DataBean dev : customType.getData()) {
            this.CustomPMC.add(dev);
            handleCustomPMC.add(dev);
        }
        setCustomPMCsAdapter();
    }

    @Override
    public void onRefresh() {

    }


    public interface ButtonSelect {
        void buttonOnclickSelect(CustomType.DataBean dataBean, int type);
    }


    public void buttonSetOnclick(ButtonSelect buttonSelect) {
        this.buttonSelect = buttonSelect;
    }


    public void initRView(View view) {

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_search);

        linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);

    }


}
